<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class c_menu extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->model('m_menu');
		if(!isset($_SESSION[SESSION_USERID])){
			redirect(base_url());
		}

	}//end Login

	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : $_GET['task'];
		switch($task){
			case "list":
				$this->menu_list();
			break;
			case "create":
				$this->menu_create();
			break;
			case "update":
				$this->menu_update();
			break;
			case "delete":
				$this->menu_delete();
			break;
			case "search":
				$this->menu_search();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}

	function index(){
		$data["title"]="menu";
		$data["content"]=$this->load->view('admin/v_menu_list',$data,TRUE);
		$this->load->view('admin/v_template',$data);
	}

	function menu_load(){
		$this->load->view('admin/v_menu_list');
	}

	function menu_add(){
		//$data["title"]="menu";
		//$data["content"]=$this->load->view('admin/v_menu_post',$data,TRUE);
		//$this->load->view('admin/v_template',$data);
		$this->load->view('admin/v_menu_post');
	}

	function menu_addedit(){
		$data = array();
		$id = $this->uri->segment(3);
		if($id != false){
			$data['dataDetail'] = $this->getDetail($id);
		}
		$this->load->view('admin/v_menu_post', $data);
	}

	function menu_list(){
		$query = $this->input->get('query');
		$pagenum = @$_GET['pagenum'];
		$pagesize = @$_GET['pagesize'];
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;

		$result=$this->m_menu->menu_list($query, $start, $end);
		echo $result;

	}


	function menu_save(){
		$id = $this->input->post('id');
		$menu = $this->input->post('menu');
		$parent = $this->input->post('parent');
		$link = $this->input->post('link');
		$task = $this->input->post('task');
		$params = array(
			'id'=>$id,
			'data'=>array(
				'menu'=>$menu,
				'parent'=>$parent,
				'link'=>$link
			),
			'task'=>$task
		);
		$result = $this->m_menu->menu_save($params);
		echo $result;
	}

	function getDetail($id){
		$sql = "
			SELECT
				menu.id,
				menu.menu,
				menu.link,
				menu.parent,
				parentmenu.menu AS parentmenu_name
			FROM menu
			LEFT JOIN menu parentmenu ON parentmenu.id = menu.id
			WHERE menu.id = ".$id."
		";
		$query = $this->db->query($sql);
		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function menu_delete(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : $id;
		$result = $this->m_menu->menu_delete($id);
		echo $result;
	}

	function menu_search(){

	}

	function menu_update(){

	}

	function menu_create(){

	}

}
?>
